package com.wangshili.sys.service.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.wangshili.common.exception.ServiceException;
import com.wangshili.pojo.common.vo.SysZTreeNode;
import com.wangshili.pojo.sys.entity.SysDept;
import com.wangshili.sys.dao.SysDeptDao;
import com.wangshili.sys.service.SysDeptService;
import com.wangshili.sys.vo.SysDeptParent;

@Service
public class SysDeptServiceImpl implements SysDeptService {

	@Autowired
	private SysDeptDao sysDeptDao;
	@Override
	public SysDept selectObjectById(Integer id) {
		if(id==null || id<1)throw new ServiceException("部门id不正确");
		SysDept selectObjectById = sysDeptDao.selectObjectById(id);
		return selectObjectById;
	}
	
	/**
	 * 查询Dept所有的部门id，name,父id封装到SysZTreeNode类里
	 * @return
	 */
	@Override
	public List<SysZTreeNode> listDeptZTreeNodes() {
		List<SysZTreeNode> listDeptZTreeNodes = 
				sysDeptDao.listDeptZTreeNodes();
		return listDeptZTreeNodes;
	}
	/**
	 * 查找全部的部门信息和上级部门名称
	 * @return SysDeptParent
	 */
	@Override
	public List<SysDeptParent> selectSysDeptParent() {
		List<SysDeptParent> selectSysDeptParent = 
				sysDeptDao.selectSysDeptParent();
		if(StringUtils.isEmpty(selectSysDeptParent))
			throw new ServiceException("没有找到数据");
		return selectSysDeptParent;
	}
	
	
	/**
	 * 根据id删除部门信息
	 * @return
	 */
	@Override
	public int deleteObjectById(Integer id) {
		int rows = 
				sysDeptDao.deleteObjectById(id);
		if(rows<1)
			throw new ServiceException("该部门可能不存在");
		return rows;
	}
	
	
	/**
	 * 插入一行数据
	 * @param entity
	 * @return
	 */
	@Override
	public int insertObject(SysDept entity) {
		if(entity==null)
			throw new IllegalArgumentException("数据不能为空");
		if(StringUtils.isEmpty(entity.getName()))
			throw new IllegalArgumentException("用户不能为空");

		int rows = 
				sysDeptDao.insertObject(entity);
		if(rows<1)
			throw new ServiceException("添加失败，请重试");
		return rows;
	}
	/**
	 * 修改一行数据
	 * @param entity
	 * @return
	 */
	@Override
	public int updateObject(SysDept entity) {
		if(entity==null)
			throw new IllegalArgumentException("数据不能为空");
		if(StringUtils.isEmpty(entity.getName()))
			throw new IllegalArgumentException("用户不能为空");
		int rows = 
				sysDeptDao.updateObject(entity);
		if(rows<1)
			throw new ServiceException("更新失败，请重试");
		return 0;
	}

}
